//
//  LSZPersonalInformationPageVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/8/9.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZPersonalInformationPageVC.h"

 
@interface LSZPersonalInformationPageVC ()<UINavigationControllerDelegate>
@property(nonatomic,strong)CustomHeaderView *titleHeaderView;
@property(nonatomic,strong)UIImageView * header;
@property(nonatomic,strong)UIImageView * imageHeader;
@property(nonatomic,strong)UILabel * labelName;
@property(nonatomic,strong)UIButton * butFocusOn;//是否关注
@property(nonatomic,strong)UIButton * butInviteCode;
@property(nonatomic,strong)UILabel  * labelInviteCode;

@property(nonatomic,strong)UIButton * butLove;
@property(nonatomic,strong)UIImageView * imageViewLove;
@property(nonatomic,strong)UILabel * labelLove;

@property(nonatomic,strong)UIButton * butFuYin;
@property(nonatomic,strong)UIImageView * imageViewFuYin;
@property(nonatomic,strong)UILabel * labelFuYin;

@end

@implementation LSZPersonalInformationPageVC
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton * butReturn = [[UIButton alloc]init];
    butReturn.frame = CGRectMake(0, STATUS_BAR_HEIGHT, 60, NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT);
    [butReturn setImage:[UIImage imageNamed:@"home_nav_button_back"] forState:UIControlStateNormal];
    [butReturn addTarget:self action:@selector(clickButReturn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:butReturn];
    [self.view bringSubviewToFront:butReturn];

    self.tabPageController.tabBar.itemTitleFont = [UIFont systemFontOfSize:15];
    self.tabPageController.tabBar.indicatorScrollFollowContent = YES;
    [self initViewControllers];
    [self informationPagei];
    [LSZRequestResolutionDic InformationThatGetsAttention:self.uid :^(BOOL isOk, NSString * string, LSZInformationConcernManager * manager) {
        if (isOk == YES) {
            [self theAssignment:manager];
        }else{
            [self initWithString:string];
        }
     }];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offset=scrollView.contentOffset.y;
    if (offset <= 0) {
        scrollView.bounces = NO;
    }
    else{
        scrollView.bounces = YES;
    }
    if (iPhoneX) {
        CGFloat height = (self.headerView.frame.size.height-STATUS_BAR_HEIGHT-NAVIGATION_BAR_HEIGHT-offset)/(self.headerView.frame.size.height-STATUS_BAR_HEIGHT-NAVIGATION_BAR_HEIGHT);
        if(offset >= self.headerView.frame.size.height-STATUS_BAR_HEIGHT-NAVIGATION_BAR_HEIGHT){
            self.scrollView.contentOffset = CGPointMake(0,self.headerView.frame.size.height-NAVIGATION_BAR_HEIGHT-STATUS_BAR_HEIGHT);//到导航的高度不滑动
            self.butLove.alpha = 0;
            self.butFuYin.alpha = 0;
            self.imageHeader.alpha = 0;
            self.labelName.alpha = 0;
            self.butFocusOn.alpha = 0;
            self.butInviteCode.alpha = 0;
        }
        if (height>=1) {
            self.butLove.alpha = 1;
            self.butFuYin.alpha = 1;
            self.imageHeader.alpha = 1;
            self.labelName.alpha = 1;
            self.butFocusOn.alpha = 1;
            self.butInviteCode.alpha = 1;
            if([self.isMine isEqualToString:@"1"]){
                self.labelInviteCode.alpha = 1;
                self.butInviteCode.alpha = 1;
                self.butFocusOn.hidden = YES;
            }else{
                self.labelInviteCode.hidden = YES;
                self.butInviteCode.hidden = YES;
                self.butFocusOn.alpha = 1;
            }
        }else{
            self.butLove.alpha = height;
            self.butFuYin.alpha = height;
            self.imageHeader.alpha = height;
            self.labelName.alpha = height;
            self.butFocusOn.alpha = height;
            self.butInviteCode.alpha = height;
            if([self.isMine isEqualToString:@"1"]){
                self.butInviteCode.alpha = height;
            }else{
                self.butFocusOn.alpha = height;
            }
        }
    }else{
        CGFloat height = (self.headerView.frame.size.height-NAVIGATION_BAR_HEIGHT-offset)/(self.headerView.frame.size.height-NAVIGATION_BAR_HEIGHT);
        if(offset >= self.headerView.frame.size.height-NAVIGATION_BAR_HEIGHT){
            self.scrollView.contentOffset = CGPointMake(0,self.headerView.frame.size.height-NAVIGATION_BAR_HEIGHT);//到导航的高度不滑动
            self.butLove.alpha = 0;
            self.butFuYin.alpha = 0;
            self.imageHeader.alpha = 0;
            self.labelName.alpha = 0;
            self.butFocusOn.alpha = 0;
            self.butInviteCode.alpha = 0;
        }
        if (height>=1) {
            self.butLove.alpha = 1;
            self.butFuYin.alpha = 1;
            self.imageHeader.alpha = 1;
            self.labelName.alpha = 1;
            self.butFocusOn.alpha = 1;
            self.butInviteCode.alpha = 1;
            if([self.isMine isEqualToString:@"1"]){
                self.labelInviteCode.alpha = 1;
                self.butInviteCode.alpha = 1;
                self.butFocusOn.hidden = YES;
            }else{
                self.labelInviteCode.hidden = YES;
                self.butInviteCode.hidden = YES;
                self.butFocusOn.alpha = 1;
            }
        }else{
            self.butLove.alpha = height;
            self.butFuYin.alpha = height;
            self.imageHeader.alpha = height;
            self.labelName.alpha = height;
            self.butFocusOn.alpha = height;
            self.butInviteCode.alpha = height;
            if([self.isMine isEqualToString:@"1"]){
                self.butInviteCode.alpha = height;

            }else{
                self.butFocusOn.alpha = height;

            }
        }
    }
}
#pragma mark - 加关注
-(void)clickFocusOn
{
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
    }else{
        [LSZRequestData payAttentionToOthers:self.uid Ck:@"0" mode:@"0" :^(BOOL isOk, NSString * str, NSDictionary * dic) {
            if (isOk == YES) {
                [self.butFocusOn setTitle:@"已关注" forState:UIControlStateNormal];
                self.butFocusOn.backgroundColor =  partialShallow;
                self.butFocusOn.userInteractionEnabled = NO;
            }else{
                [self initWithString:str];
            }
        }];
    }
}
-(void)theAssignment:(LSZInformationConcernManager *)manager
{
    self.labelName.text = manager.username;
    if(manager.is_follow == 1){
        [self.butFocusOn setTitle:@"已关注" forState:UIControlStateNormal];
        self.butFocusOn.backgroundColor =  partialShallow;
        self.butFocusOn.userInteractionEnabled = NO;
    }else{
        [self.butFocusOn setTitle:@"+关注" forState:UIControlStateNormal];
        self.butFocusOn.backgroundColor =  redUIColor;
        self.butFocusOn.userInteractionEnabled = YES;
    }
    if ([manager.credit isEqualToString:@""]) {
        self.labelFuYin.text = [NSString stringWithFormat:@"福音值 0.00"];
    }else{
        self.labelFuYin.text = [NSString stringWithFormat:@"福音值 %@",manager.credit];
    }
    if ([manager.score isEqualToString:@""]) {
        self.labelLove.text = [NSString stringWithFormat:@"爱心值 0.00"];
    }else{
        self.labelLove.text = [NSString stringWithFormat:@"爱心值 %@",manager.score];
    }
    CGFloat lovelabel = [LSZSpecialPackaging getsTheAdaptiveWidth:self.labelLove sizeMaxWidth:ScreenWidth/2];
    CGFloat fuYinLabel = [LSZSpecialPackaging getsTheAdaptiveWidth:self.labelFuYin sizeMaxWidth:ScreenWidth/2];
    self.labelLove.frame = CGRectMake((ScreenWidth/2-lovelabel)/2, self.labelLove.frame.origin.y, lovelabel, self.labelLove.frame.size.height);
    self.labelFuYin.frame = CGRectMake((ScreenWidth/2-fuYinLabel)/2, self.labelFuYin.frame.origin.y, fuYinLabel, self.labelFuYin.frame.size.height);

    CGRect rect = self.imageViewLove.frame;
    rect.origin.x = CGRectGetMinX(self.labelLove.frame)-self.imageViewLove.frame.size.width-2;
    self.imageViewLove.frame = rect;

    CGRect rect2 = self.imageViewFuYin.frame;
    rect2.origin.x = CGRectGetMinX(self.labelFuYin.frame)-self.imageViewFuYin.frame.size.width-2;
    self.imageViewFuYin.frame = rect2;
}
-(void)informationPagei
{
    self.imageHeader = [[UIImageView alloc]init];
    [self.imageHeader sd_setImageWithURL:[NSURL URLWithString:self.imageViewHeader] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    [self.imageHeader.layer setMasksToBounds:YES];
    [self.header addSubview:self.imageHeader];

    self.labelName = [[UILabel alloc]init];
    self.labelName.textColor = [UIColor whiteColor];
    self.labelName.text = @"名字";
    [LSZSpecialPackaging setLabelAdaptiveSize:self.labelName font:16];
    [self.header addSubview:self.labelName];
  
    
    self.butFocusOn =[[UIButton alloc]init];
    [self.butFocusOn.layer setMasksToBounds:YES];
    self.butFocusOn.backgroundColor = redUIColor;
    [self.butFocusOn setTitle:@"+关注" forState:UIControlStateNormal];
    [LSZSpecialPackaging setLabelAdaptiveSize:self.butFocusOn.titleLabel font:16];
    [self.butFocusOn addTarget:self action:@selector(clickFocusOn) forControlEvents:UIControlEventTouchUpInside];
    [self.header addSubview:self.butFocusOn];
//二选一
    self.butInviteCode = [[UIButton alloc]init];
    [self.butInviteCode setImage:[UIImage imageNamed:@"我的_我的邀请码"] forState:UIControlStateNormal];
    [self.butInviteCode addTarget:self action:@selector(clickInformation) forControlEvents:UIControlEventTouchUpInside];
    [self.header addSubview:self.butInviteCode];

    self.labelInviteCode = [[UILabel alloc]init];
    self.labelInviteCode.textColor = [UIColor whiteColor];
    self.labelInviteCode.textAlignment = NSTextAlignmentCenter;
    self.labelInviteCode.text = @"信息资料";
    [LSZSpecialPackaging setLabelAdaptiveSize:self.labelInviteCode font:13.5];
    [self.labelInviteCode tapGesture:^(UIGestureRecognizer *ges) {
        [self clickInformation];
    }];
    [self.butInviteCode addSubview:self.labelInviteCode];
    if([self.isMine isEqualToString:@"1"]){
        self.labelInviteCode.hidden = NO;
        self.butInviteCode.hidden = NO;
        self.butFocusOn.hidden = YES;
    }else{
        self.labelInviteCode.hidden = YES;
        self.butInviteCode.hidden = YES;
        self.butFocusOn.hidden = NO;
    }

    //爱心值
    self.butLove = [[UIButton alloc]init];
    [self.header addSubview:self.butLove];

    self.imageViewLove = [[UIImageView alloc]init];
    self.imageViewLove.image = [UIImage imageNamed:@"我的_爱心值"];
    [self.butLove addSubview:self.imageViewLove];

    self.labelLove = [[UILabel alloc]init];
    self.labelLove.textColor = [UIColor whiteColor];
    [self.butLove addSubview:self.labelLove];
    //福音值
    self.butFuYin = [[UIButton alloc]init];
    [self.header addSubview:self.butFuYin];

    self.imageViewFuYin = [[UIImageView alloc]init];
    self.imageViewFuYin.image = [UIImage imageNamed:@"我的_福音值"];
    [self.butFuYin addSubview:self.imageViewFuYin];

    self.labelFuYin = [[UILabel alloc]init];
    self.labelFuYin.textColor = [UIColor whiteColor];
    [self.butFuYin addSubview:self.labelFuYin];
    if (ScreenWidth == 320) {
        self.imageHeader.frame = CGRectMake(10, NAVIGATION_BAR_HEIGHT+10, 50, 50);
        self.labelName.frame = CGRectMake(CGRectGetMaxX(self.imageHeader.frame)+10, CGRectGetMinY(self.imageHeader.frame)+(self.imageHeader.frame.size.height-18)/2, ScreenWidth/2, 18);
        self.butFocusOn.frame = CGRectMake(ScreenWidth-100, CGRectGetMinY(self.imageHeader.frame)+(self.imageHeader.frame.size.height-35)/2, 80, 35);
        [self.butFocusOn.layer setCornerRadius:4];
        self.butInviteCode.frame = CGRectMake(ScreenWidth-100, CGRectGetMinY(self.imageHeader.frame)+(self.imageHeader.frame.size.height-35)/2, 100, 35);
        self.labelInviteCode.frame = CGRectMake(26,0,self.butInviteCode.frame.size.width-35, self.butInviteCode.frame.size.height);


        self.butLove.frame = CGRectMake(0, self.header.frame.size.height-60, ScreenWidth/2,60);
        self.imageViewLove.frame = CGRectMake(115/4,(self.butLove.frame.size.height-11)/2-1, 12, 11);
        self.labelLove.frame = CGRectMake(CGRectGetMaxX(self.imageViewLove.frame)+3, 20, self.butLove.frame.size.width-CGRectGetMaxX(self.imageViewLove.frame)-5, 6+11);

        self.butFuYin.frame = CGRectMake(ScreenWidth/2, self.header.frame.size.height-60, ScreenWidth/2,60);
        self.imageViewFuYin.frame = CGRectMake(115/4,(self.butFuYin.frame.size.height-11)/2-1, 12, 11);
        self.labelFuYin.frame = CGRectMake(CGRectGetMaxX(self.imageViewFuYin.frame)+3, 20, self.butFuYin.frame.size.width-CGRectGetMaxX(self.imageViewFuYin.frame)-5, 6+11);

    }else if(ScreenWidth == 375){
        self.imageHeader.frame = CGRectMake(10, NAVIGATION_BAR_HEIGHT+10, 55, 55);
        self.labelName.frame = CGRectMake(CGRectGetMaxX(self.imageHeader.frame)+10, CGRectGetMinY(self.imageHeader.frame)+(self.imageHeader.frame.size.height-18)/2, ScreenWidth/2, 20);
        self.butFocusOn.frame = CGRectMake(ScreenWidth-100, CGRectGetMinY(self.imageHeader.frame)+(self.imageHeader.frame.size.height-37)/2, 80, 37);
        [self.butFocusOn.layer setCornerRadius:5];
        self.butInviteCode.frame = CGRectMake(ScreenWidth-110, CGRectGetMinY(self.imageHeader.frame)+(self.imageHeader.frame.size.height-37)/2, 110, 37);
        self.labelInviteCode.frame = CGRectMake(34,0,self.butInviteCode.frame.size.width-50, self.butInviteCode.frame.size.height);

        self.butLove.frame = CGRectMake(0,  self.header.frame.size.height-65, ScreenWidth/2, 65);
        self.imageViewLove.frame = CGRectMake(115/4,(self.butLove.frame.size.height-11)/2-3, 12, 11);
        self.labelLove.frame = CGRectMake(CGRectGetMaxX(self.imageViewLove.frame)+3, 20, self.butLove.frame.size.width-CGRectGetMaxX(self.imageViewLove.frame)-5, 6+11);

        self.butFuYin.frame = CGRectMake(ScreenWidth/2, self.header.frame.size.height-65, ScreenWidth/2, 65);
        self.imageViewFuYin.frame = CGRectMake(115/4,(self.butFuYin.frame.size.height-11)/2-3, 12, 11);
        self.labelFuYin.frame = CGRectMake(CGRectGetMaxX(self.imageViewFuYin.frame)+3, 20, self.butFuYin.frame.size.width-CGRectGetMaxX(self.imageViewFuYin.frame)-5, 6+11);

    }else {
        self.imageHeader.frame = CGRectMake(10, NAVIGATION_BAR_HEIGHT+10, 60, 60);
        self.labelName.frame = CGRectMake(CGRectGetMaxX(self.imageHeader.frame)+10, CGRectGetMinY(self.imageHeader.frame)+(self.imageHeader.frame.size.height-18)/2, ScreenWidth/2, 22);
        self.butFocusOn.frame = CGRectMake(ScreenWidth-100, CGRectGetMinY(self.imageHeader.frame)+(self.imageHeader.frame.size.height-39)/2, 80, 39);
        [self.butFocusOn.layer setCornerRadius:6];
        self.butInviteCode.frame = CGRectMake(ScreenWidth-115,CGRectGetMinY(self.imageHeader.frame)+(self.imageHeader.frame.size.height-39)/2, 115, 39);
        self.labelInviteCode.frame = CGRectMake(30,0,self.butInviteCode.frame.size.width -50, self.butInviteCode.frame.size.height);

        self.butLove.frame = CGRectMake(0, self.header.frame.size.height-73, ScreenWidth/2, 73);
        self.imageViewLove.frame = CGRectMake(115/4, (self.butLove.frame.size.height-13*1.1)/2, 12*1.1, 11*1.1);
        self.labelLove.frame = CGRectMake(CGRectGetMaxX(self.imageViewLove.frame)+3,(self.butLove.frame.size.height-13*1.1)/2-4, self.butLove.frame.size.width-CGRectGetMaxX(self.imageViewLove.frame)-5, 8+11);

        self.butFuYin.frame = CGRectMake(ScreenWidth/2, self.header.frame.size.height-73, ScreenWidth/2, 73);
        self.imageViewFuYin.frame = CGRectMake(115/4, (self.butLove.frame.size.height-13*1.1)/2, 12*1.1, 11*1.1);
        self.labelFuYin.frame = CGRectMake(CGRectGetMaxX(self.imageViewFuYin.frame)+3,(self.butFuYin.frame.size.height-13*1.1)/2-4,self.butFuYin.frame.size.width-CGRectGetMaxX(self.imageViewFuYin.frame)-5, 8+11);
    }
    [self.imageHeader.layer setCornerRadius:self.imageHeader.frame.size.width/2];
}
- (void)initViewControllers{
    NSMutableArray *vcArray = [NSMutableArray array];
    NSArray * array = @[@"音频",@"动态",@"关注",@"粉丝"];
    int i = 0;
    while (i<array.count) {
        if (i == 0) {
            LSZAudioCollectionViewController *normal = [[LSZAudioCollectionViewController alloc] init];
            normal.yp_tabItemTitle = [NSString stringWithFormat:@"%@",array[i]];
            normal.uid = self.uid;
            [vcArray addObject:normal];
        }
        else if (i == 1){
            LSZNearViewController * vc = [[LSZNearViewController alloc]init];
            vc.yp_tabItemTitle = [NSString stringWithFormat:@"%@",array[i]];
            vc.is_city = @"";
            vc.is_follow = @"";
            vc.fuid = self.uid;
            vc.comeFrome = @"1";
            [vcArray addObject:vc];
        }
        else{
            LSZFansViewController * controller = [[LSZFansViewController alloc]init];
            if (i == 2){
                controller.urlString = @"0";
            }else{
                controller.urlString = @"1";
            }
            controller.fuid = self.uid;
            controller.yp_tabItemTitle = [NSString stringWithFormat:@"%@",array[i]];
            [vcArray addObject:controller];
        }
        i++;
    }
    self.tabPageController.viewControllers = vcArray;
}
- (UIImageView *)headerView {
    self.header  = [[UIImageView alloc] init];
    if (ScreenWidth == 320) {
        self.header.frame = CGRectMake(0, 0, ScreenWidth, 200);
    }else if(ScreenWidth == 375){
        if (iPhoneX) {
            self.header.frame = CGRectMake(0,-STATUS_BAR_HEIGHT, ScreenWidth, 210);
        }else{
            self.header.frame = CGRectMake(0, 0, ScreenWidth, 210);
        }
    }else {
        self.header.frame = CGRectMake(0, 0, ScreenWidth, 220);
    }
    self.header.image = [UIImage imageNamed:@"个人信息背景"];
    self.header.userInteractionEnabled = YES;
    return self.header;
}
#pragma mark - 跳转到个人信息界面
-(void)clickInformation
{
    LSZPersonalInformationVC * vc = [[LSZPersonalInformationVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)clickButReturn
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
#pragma mark - 弹出警告框
-(void)initWithString :(NSString *)string
{
    AlertKuangView * alert = [AlertKuangView new];
    [alert moveJingGaoKuang:string:self.view];
}
- (BOOL)prefersStatusBarHidden {
    return YES;
}
@end
